﻿using System;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Istace.Framework.Mapping.Core.Interfaces;

namespace Istace.Framework.Mapping.Core.Exceptions
{
    /// <summary>
    /// Base exception type for repository exceptions. Filtering purpose.
    /// </summary>
    [Serializable]
    public class RepositoryBaseException<T> : Exception
    {
        /// <summary>
        /// The object for which the exception occur
        /// </summary>
        private object IndexedObject { get; set; }

        /// <summary>
        /// The key index of the object for which the exception occur
        /// </summary>
        private string ObjectIndex { get; set; }

        /// <summary>
        /// The repository which throw the exception
        /// </summary>
        private IIndexedRepository<T> TargetRepositoryInstance { get; set; }

        /// <summary>
        /// Default .ctor
        /// </summary>
        /// <param name="message">The exception message</param>
        /// <param name="indexedInstance">The object for which the exception occur</param>
        /// <param name="objectIndex">The key index of the object for which the exception occur</param>
        /// <param name="targetRepositoryInstance">The repository which throw the exception</param>
        public RepositoryBaseException(string message, object indexedInstance, string objectIndex, IIndexedRepository<T> targetRepositoryInstance)
            : base(message)
        {
            this.IndexedObject = indexedInstance;
            this.ObjectIndex = objectIndex;
            this.TargetRepositoryInstance = targetRepositoryInstance;
        }

        #region Exception .ctor
        /// <summary>
        /// Initializes a new instance of the <see cref="RepositoryBaseException{T}"/> class.
        /// </summary>
        public RepositoryBaseException()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RepositoryBaseException{T}" /> class.
        /// </summary>
        /// <param name="message">The message that describes the error.</param>
        public RepositoryBaseException(string message) 
            : base(message)
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RepositoryBaseException{T}"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="exception">The exception.</param>
        public RepositoryBaseException(string message, Exception exception) 
            : base(message, exception)
        {
            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RepositoryBaseException{T}"/> class.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
        protected RepositoryBaseException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {

        }
        #endregion

        #region ISerializable Implementation
        /// <summary>
        /// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo" /> with information about the exception.
        /// </summary>
        /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
        /// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*" />
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter" />
        ///   </PermissionSet>
        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if(info == null) throw new ArgumentNullException("info");
            info.AddValue("IndexedObject", IndexedObject);
            info.AddValue("ObjectIndex", ObjectIndex);
            info.AddValue("TargetRepositoryInstance", TargetRepositoryInstance);
            base.GetObjectData(info, context);
        }
        #endregion
    }
}
